using System;
using System.Collections;
using System.DirectoryServices;

namespace BEGDC.CC.Entity
{
	#region FlowInstance

	/// <summary>
	/// FlowInstance object for NHibernate mapped table 'FlowInstance'.
	/// </summary>
	[Serializable]
	public class FlowInstance : System.IComparable
		{
		#region Member Variables
		
		protected int _id;
		protected long _flowID;
		protected string _currentOwner;
		protected string _currentActiveOwner;
		protected Nullables.NullableDateTime _startDate;
		protected Nullables.NullableDateTime _lastModifiedDate;
		protected bool _isCancelled;
		protected Application _application;
		protected State _state;
		protected Step _currentStep;
		protected Step _currentActiveStep;
		protected static String _sortExpression = "Id";
		protected static SortDirection _sortDirection = SortDirection.Ascending;

		#endregion

		#region Constructors

		public FlowInstance() { }

		public FlowInstance( long flowID, string currentOwner, string currentActiveOwner, Nullables.NullableDateTime startDate, Nullables.NullableDateTime lastModifiedDate, bool isCancelled, Application application, State state, Step currentStep, Step currentActiveStep )
		{
			this._flowID = flowID;
			this._currentOwner = currentOwner;
			this._currentActiveOwner = currentActiveOwner;
			this._startDate = startDate;
			this._lastModifiedDate = lastModifiedDate;
			this._isCancelled = isCancelled;
			this._application = application;
			this._state = state;
			this._currentStep = currentStep;
			this._currentActiveStep = currentActiveStep;
		}

		#endregion

		#region Public Properties

		public int Id
		{
			get {return _id;}
			set {_id = value;}
		}

		public long FlowID
		{
			get { return _flowID; }
			set { _flowID = value; }
		}

		public string CurrentOwner
		{
			get { return _currentOwner; }
			set
			{
				if ( value != null && value.Length > 20)
					throw new ArgumentOutOfRangeException("Invalid value for CurrentOwner", value, value.ToString());
				_currentOwner = value;
			}
		}

		public string CurrentActiveOwner
		{
			get { return _currentActiveOwner; }
			set
			{
				if ( value != null && value.Length > 20)
					throw new ArgumentOutOfRangeException("Invalid value for CurrentActiveOwner", value, value.ToString());
				_currentActiveOwner = value;
			}
		}

		public Nullables.NullableDateTime StartDate
		{
			get { return _startDate; }
			set { _startDate = value; }
		}

		public Nullables.NullableDateTime LastModifiedDate
		{
			get { return _lastModifiedDate; }
			set { _lastModifiedDate = value; }
		}

		public bool IsCancelled
		{
			get { return _isCancelled; }
			set { _isCancelled = value; }
		}

		public Application Application
		{
			get { return _application; }
			set { _application = value; }
		}

		public State State
		{
			get { return _state; }
			set { _state = value; }
		}

		public Step CurrentStep
		{
			get { return _currentStep; }
			set { _currentStep = value; }
		}

		public Step CurrentActiveStep
		{
			get { return _currentActiveStep; }
			set { _currentActiveStep = value; }
		}

        public static String SortExpression
        {
            get { return _sortExpression; }
            set { _sortExpression = value; }
        }

        public static SortDirection SortDirection
        {
            get { return _sortDirection; }
            set { _sortDirection = value; }
        }
		#endregion
		
        #region IComparable Methods
        public int CompareTo(object obj)
        {
			if (!(obj is FlowInstance))
				throw new InvalidCastException("This object is not of type FlowInstance");
			
			int relativeValue;
			switch (SortExpression)
			{
				case "Id":
					relativeValue = this.Id.CompareTo(((FlowInstance)obj).Id);
					break;
				case "FlowID":
					relativeValue = this.FlowID.CompareTo(((FlowInstance)obj).FlowID);
					break;
				case "CurrentOwner":
						relativeValue = ( this.CurrentOwner != null ) ? this.CurrentOwner.CompareTo(((FlowInstance)obj).CurrentOwner) : -1;
					break;
				case "CurrentActiveOwner":
						relativeValue = ( this.CurrentActiveOwner != null ) ? this.CurrentActiveOwner.CompareTo(((FlowInstance)obj).CurrentActiveOwner) : -1;
					break;
				case "StartDate":
						relativeValue = ( this.StartDate != null ) ? this.StartDate.CompareTo(((FlowInstance)obj).StartDate) : -1;
					break;
				case "LastModifiedDate":
						relativeValue = ( this.LastModifiedDate != null ) ? this.LastModifiedDate.CompareTo(((FlowInstance)obj).LastModifiedDate) : -1;
					break;
				case "IsCancelled":
					relativeValue = this.IsCancelled.CompareTo(((FlowInstance)obj).IsCancelled);
					break;
                default:
                    goto case "Id";
			}
            if (FlowInstance.SortDirection == SortDirection.Ascending)
				relativeValue *= -1;
			return relativeValue;
		}
		#endregion
	}

	#endregion
}
